package com.ktjiaoyu.interceptors;

import com.ktjiaoyu.entity.SysUser;
import com.ktjiaoyu.utils.Constants;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.HttpSession;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.servlet.HandlerInterceptor;

public class AuthInterceptor implements HandlerInterceptor {
    private static final Logger LOGGER = LoggerFactory.getLogger(AuthInterceptor.class);

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        LOGGER.debug("进入自定义拦截器AuthInterceptor,请求路径为:{}", request.getRequestURI());
        HttpSession session = request.getSession();
        // 从session中获取用户信息
        SysUser loginUser = (SysUser) session.getAttribute(Constants.SESSION_USER);
        if (loginUser == null) {
            request.getRequestDispatcher("/WEB-INF/jsp/401.jsp").forward(request, response);
            return false;
        }
        return true;
    }
}
